<?php
//require_once 'SubjectInterface.php';
//require_once '../Observer/ObserverInterface.php';
class WeatherSubject implements SubjectInterface {
    private $observer;
    private $change = false;
    private $temperature;
    private $humidity;
    public function registerObserver(ObserverInterface $observer) {
        if (!empty($this->observer))
        foreach ($this->observer as $ob)
        {
            if ($ob === $observer)
            {
                return;
            }
        }
        $this->observer[] = $observer;
    }
    public function removeObserver(ObserverInterface $observer) {
        foreach ($this->observer as $key => $ob)
        {
            if ($ob === $observer)
            {
                unset($this->observer[$key]);
            }
        }
    }
    public function notifyObserver() {
        if ($this->change)
        {
            foreach ($this->observer as $ob)
            {
                $ob->update($this);
            }
        }
        $this->change = false;
    }

    public function setChange()
    {
        $this->change = true;
    }

    //设置温度
    public function setTemperature(float $temperature) {
        $this->temperature = $temperature;
        //通知观察者更新
        $this->notifyObserver();
    }

    //设置湿度
    public function setHumidity(float $humidity) {
        $this->humidity = $humidity;
        //通知观察者更新
        $this->notifyObserver();
    }

    //温度
    public function getTemperature() {
        return $this->temperature;
    }

    //湿度
    public function getHumidity()
    {
        return $this->humidity;
    }
}